﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace server.app
{
    public sealed class ItemManager
    {
        World _world = null;

        public ItemManager(World world)
        {
            _world = world;
        }

        public string[] CreateItem(string playerId, comm.objst.CreateItem createItem)
        {
            return _world.DataManager.Call<string[]>(sqlx =>
            {
                sqlx.BeginTxn();
                var ids = CreateItem(sqlx, playerId, createItem);
                if (ids != null)
                {
                    sqlx.CommitTxn();
                }
                return ids;
            });
        }
        public string[] CreateItem(SQLx sqlx, string playerId, comm.objst.CreateItem createItem)
        {
            var dItem = _world.DefineFile.GetItem(createItem.ItemId);
            if (dItem == null)
            {
                Console.WriteLine("dItemId:" + createItem.ItemId + " not found", comm.ConsoleColor.RED);
                return null;
            }
            var itemMax = BackpackManager.PACK_ITEM_MAX;
            var list = new List<string>();
            var countList = new List<int>();

            #region calc count
            if (dItem.PackItemOnly)
            {
                for (int i = 0; i < createItem.Count; i++)
                {
                    countList.Add(1);
                }
            }
            else
            {
                int c = (int)(createItem.Count / itemMax), c2 = (int)(createItem.Count - c * itemMax);
                for (int i = 0; i < c; i++)
                {
                    countList.Add(itemMax);
                }
                if (c2 != 0)
                {
                    countList.Add(c2);
                }
            }
            #endregion

            var item = new comm.objst.TableItem
            {
                DItemId = createItem.ItemId,
                PlayerId = playerId,
            };

            for (int i = 0, len = countList.Count; i < len; i++)
            {
                item.ItemId = createItem.ItemId + "_" + comm.GUID.NewString();
                item.ItemCount = countList[i];

                list.Add(item.ItemId);
                if (!_world.DataManager.TableItem.CreateItem(sqlx, item))
                {
                    return null;
                }
            }
            return list.ToArray();
        }

        public comm.objst.TableItem GetItem(string itemId)
        {
            return _world.DataManager.TableItem.GetItem(itemId);
        }

        public comm.objst.TableItem[] GetAllItem(string[] itemIds)
        {
            return _world.DataManager.TableItem.GetAllItem(itemIds);
        }
        public Dictionary<string, comm.objst.TableItem> GetAllItemDic(string[] itemIds)
        {
            var all = GetAllItem(itemIds);
            var dic = new Dictionary<string, comm.objst.TableItem>();
            foreach (var item in all)
            {
                dic.Add(item.ItemId, item);
            }
            return dic;
        }

        public string ItemToBackpack(string playerId, string[] itemIds)
        {
            return _world.DataManager.Call<string>(sqlx =>
            {
                sqlx.BeginTxn();
                var r = ItemToBackpack(sqlx, playerId, itemIds);
                if (r == comm.CodeStr.OK)
                {
                    sqlx.CommitTxn();
                }
                return r;
            });
        }
        public string ItemToBackpack(SQLx sqlx, string playerId, string[] itemIds)
        {
            var items = _world.DataManager.TableItem.GetAllItem(sqlx, itemIds);
            var list = new List<string>();
            foreach (var item in items)
            {
                var dItem = _world.DefineFile.GetItem(item.DItemId);
                if (dItem != null)
                {
                    list.Add(item.ItemId);
                }
            }
            var code = _world.BackpackManager.AddItem(sqlx, playerId, list.ToArray());
            if (code != comm.CodeStr.OK)
            {
                return code;
            }
            return comm.CodeStr.OK;
        }
    }
}
